home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / menu.c < prev    next >
Encoding:
Text File  |  1998-06-06  |  5.5 KB  |  202 lines  |  [TEXT/CWIE]

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5. #include <Speech.h>
  6. #include <Ctype.h>
  7. #include <DeskBus.h>
  8. #include <Retrace.h>
  9.  
  10. #include "filter.h"
  11. #include "adb.h"
  12. #include "pref.h"
  13. #include "globals.h"
  14. #include "speech.h"
  15. #include "menu.h"
  16. #include "about.h"
  17. #include "command.h"
  18. #include "aerecord.h"
  19.  
  20. typedef struct {
  21.     long theMenuAndItem;
  22.     CommandType command;
  23. } MenuCommand;
  24.  
  25. #define MENU_ITEM(m,i)        ((((unsigned long)m)<<16) | (i & 0xFFFF))
  26. #define kAnyMenuItem        -1
  27.  
  28. MenuCommand MenusCmds[] = {
  29.     { MENU_ITEM(kAppleMenuID, kAboutItem),             AboutCmd },
  30.     { MENU_ITEM(kAppleMenuID, kAnyMenuItem),         STRING_COMMAND(OpenDescAccCmd) },
  31.     { MENU_ITEM(kFileMenuID,  kFileMenuQuitItem),    CHANGEVALUE(QuitCmd, kTrueValue) },
  32.     { MENU_ITEM(kVoiceMenuID, kAnyMenuItem),         STRING_COMMAND(VoiceCmd) },
  33.     { MENU_ITEM(kSpeakMenuID, kSpeakCharsItem),        TOGGLE_COMMAND(SpeakCharsCmd) },
  34.     { MENU_ITEM(kSpeakMenuID, kSpeakWordsItem),     TOGGLE_COMMAND(SpeakWordsCmd) },
  35.     { MENU_ITEM(kSpeakMenuID, kSpeakSentenceItem),     TOGGLE_COMMAND(SpeakSentencesCmd) },
  36. };
  37. #define kNumMenuCommands sizeof(MenusCmds)/sizeof(MenuCommand)
  38.  
  39.  
  40. // * ****************************************************************************** *
  41. // *    MenusInit
  42. // *                        init our menus
  43. // * ****************************************************************************** *
  44. OSErr MenusInit(void)
  45. {
  46.     MenuHandle    themenu;
  47.     VoiceDescription info;
  48.     VoiceSpec voiceSpec;
  49.     short count, voiceCount;
  50.     OSErr err;
  51.  
  52.     InsertMenu(GetMenu(kAppleMenuID), 0);
  53.     AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
  54.     InsertMenu(GetMenu(kFileMenuID), 0);
  55.  
  56.     // Build the Voice menu
  57.     err = CountVoices(&voiceCount);
  58.     if (!err && voiceCount) {
  59.         themenu = GetMenu(kVoiceMenuID);
  60.         InsertMenu(themenu, 0);
  61.         for (count = 1; count <= voiceCount; count++) {
  62.             err = GetIndVoice(count, &voiceSpec);
  63.             if (err) continue;
  64.  
  65.             // get the name of the voice
  66.             err = GetVoiceDescription(&voiceSpec, &info, sizeof(VoiceDescription));
  67.             if (err) continue;
  68.  
  69.             // add the name to the menu
  70.             if (themenu)
  71.                 AppendMenu(themenu, info.name);
  72.  
  73.             // Pick default voice 
  74.             if (EqualString(info.name,(**gPrefs).voice, false, false))
  75.                 gVoiceItem = count;
  76.         }
  77.  
  78.         // Just in case our default is not installed
  79.         if (gVoiceItem < 0)
  80.             gVoiceItem = 1;
  81.  
  82.         // put a check mark for the voice name in the menu
  83.         themenu = GetMenu(kVoiceMenuID);
  84.         CheckItem(themenu, gVoiceItem, true);
  85.         err = GetIndVoice(gVoiceItem, &voiceSpec);
  86.         if (!err) {
  87.             err = NewSpeechChannel(&voiceSpec, &gSpeechChannel);
  88.         }
  89.     }
  90.     themenu = GetMenu(kSpeakMenuID);
  91.     InsertMenu(themenu, 0);
  92.     
  93.     CheckItem(themenu, kSpeakCharsItem, (**gPrefs).speakChars);
  94.     CheckItem(themenu, kSpeakWordsItem, (**gPrefs).speakWords);
  95.     CheckItem(themenu, kSpeakSentenceItem, (**gPrefs).speakSentence);
  96.  
  97.     DrawMenuBar();
  98.     return noErr;
  99. }
  100.  
  101. // * ****************************************************************************** *
  102. // *    SetDefaultVoice
  103. // *                        Set voice to string stored in pref
  104. // * ****************************************************************************** *
  105. OSErr SetDefaultVoice(void)
  106. {
  107.     MenuHandle    menuHandle;
  108.     VoiceDescription info;
  109.     VoiceSpec voiceSpec;
  110.     short count, voiceCount;
  111.     OSErr err;
  112.  
  113.     menuHandle = GetMenu(kVoiceMenuID);
  114.     err = CountVoices(&voiceCount);
  115.     if (!err && voiceCount) {
  116.         for (count = 1; count <= voiceCount; count++) {
  117.             err = GetIndVoice(count, &voiceSpec);
  118.             if (err) continue;
  119.  
  120.             // get the name of the voice
  121.             err = GetVoiceDescription(&voiceSpec, &info, sizeof(VoiceDescription));
  122.             if (err) continue;
  123.  
  124.             // Pick default voice 
  125.             if (EqualString(info.name,(**gPrefs).voice, false, false)) {
  126.                 // avoid double takes from appleEvents
  127.                 if (count == gVoiceItem)
  128.                     return noErr;
  129.  
  130.                 CheckItem(menuHandle, gVoiceItem, false);
  131.                 gVoiceItem = count;
  132.  
  133.                 CheckItem(menuHandle, gVoiceItem, true);
  134.                 if (gSpeechChannel) {
  135.                     StopSpeech(gSpeechChannel);
  136.                     DisposeSpeechChannel(gSpeechChannel);
  137.                     gSpeechChannel = nil;
  138.                 }
  139.                 err = GetIndVoice(gVoiceItem, &voiceSpec);
  140.                 if (!err) 
  141.                     err = NewSpeechChannel(&voiceSpec, &gSpeechChannel);
  142.                 return err;
  143.             }
  144.         }
  145.     }
  146.     return -1;
  147. }
  148.  
  149.  
  150. // * ****************************************************************************** *
  151. // *    DoMenuItem
  152. // *                            Handle a menu action
  153. // * ****************************************************************************** *
  154. void DoMenuItem(long theMenuAndItem)
  155. {
  156.     short theMenu, theItem, cmdMenu, cmdItem, titleLen, count;
  157.     char buffer[sizeof(Str255) + sizeof(Str255)];
  158.     Str255 theString;
  159.     MenuHandle menuHandle;
  160.     MenuCommand* curMenu;
  161.     OSErr err;
  162.  
  163.     if (! theMenuAndItem) return;
  164.  
  165.     theMenu = HiWord(theMenuAndItem);
  166.     theItem = LoWord(theMenuAndItem);
  167.     menuHandle = GetMenuHandle(theMenu);
  168.  
  169.     // fill in buffer so we can speak menu title and menu item
  170.     if (menuHandle) {
  171.         titleLen =  (**menuHandle).menuData[0];
  172.         BlockMoveData(&((**menuHandle).menuData[1]), buffer, titleLen);
  173.         GetMenuItemText(menuHandle, theItem, theString);
  174.     }
  175.     else {
  176.         titleLen = 0;
  177.         *buffer = 0;
  178.         theString[0] = 0;
  179.     }
  180.  
  181.     buffer[titleLen] = ' ';
  182.     titleLen++;
  183.     BlockMoveData(&theString[1], &buffer[titleLen], theString[0]);
  184.     titleLen += theString[0];
  185.  
  186.     for (count = 0, curMenu = MenusCmds; count < kNumMenuCommands; curMenu++, count++) {
  187.         cmdMenu = HiWord(curMenu->theMenuAndItem);
  188.         if (cmdMenu == theMenu) {
  189.             cmdItem = LoWord(curMenu->theMenuAndItem);
  190.             if ((cmdItem == kAnyMenuItem) || (cmdItem == theItem)) {
  191.                 ProcessCommand(curMenu->command, theString);
  192.                 err = SavePrefs();
  193.                 if (gSpeechChannel)
  194.                     SpeakText(gSpeechChannel, buffer, titleLen);
  195.                 break;
  196.             }
  197.         } 
  198.     }
  199.     HiliteMenu(0);
  200.     DrawMenuBar();
  201. }
  202.